package com.agentlab.imitation.service;

import java.io.Serializable;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.SessionFactoryUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.agentlab.imitation.bean.WishList;
import com.agentlab.imitation.bean.WishListId;
import com.agentlab.imitation.service.base.DaoSupport;
import com.agentlab.imitation.util.ToString;

@Service
@Transactional
public class WishListServiceBean extends DaoSupport<WishList> implements WishListService {
	@Resource
	SessionFactory factory;

	public void delete(Serializable... entityids) {
		// TODO Auto-generated method stub
		if (entityids != null && entityids.length > 0) {

			for (int i = 0; i < entityids.length; i++) {
				WishListId id = (WishListId) entityids[i];
				System.out.println("WishListServiceBean.delete:WishListId=" + new ToString(id));
				// String HQL =
				// "update Contact set relation_id = 1, updateTime = :updateTime where host = :hostEmail and guest = :guestEmail";
				String HQL = "delete from  WishList  where userid = :userid and pc = :pc and yxzyxh = :yxzyxh and zyzyxh = :zyzyxh";
				Session session = factory.getCurrentSession();
				Query query = session.createQuery(HQL);
				query.setInteger("userid", id.getUserid());
				query.setString("pc", id.getPc());
				query.setString("yxzyxh", id.getYxzyxh());
				query.setString("zyzyxh", id.getZyzyxh());
				query.executeUpdate();
				// session.close();
				SessionFactoryUtils.releaseSession(session, factory);
			}
		}

	}

	public List<WishList> getListByUserId(Integer userid) {
		List list = factory.getCurrentSession().createQuery(" from WishList u where u.id.userid=:userid ").setParameter("userid", userid).list();
		List<WishList> wishs = list;
		return wishs;
	}

	/**
	 * 根据用户ID和批次找到志愿
	 * 
	 * @param userid
	 * @return
	 */
	public List<WishList> getListByUserIdAndPc(Integer userid, String pc) {
		List list = factory.getCurrentSession().createQuery(" from WishList u where u.id.userid=:userid and id.pc=:pc")
				.setParameter("userid", userid).setParameter("pc", pc).list();
		List<WishList> wishs = list;
		return wishs;
	}

	/**
	 * 保存或修改
	 */
	public void saveOrUpdate(WishList wishList) {
		if (this.find(wishList.getId()) != null) {
			this.update(wishList);
		} else {
			this.save(wishList);
		}
	}

	public List<WishList> getListByUserIdPcYxzyxh(Integer userid, String pc, String yxzyxh) {
		// TODO Auto-generated method stub
		List list = factory.getCurrentSession().createQuery(" from WishList u where u.id.userid=:userid and id.pc=:pc and id.yxzyxh=:yxzyxh")
				.setParameter("userid", userid).setParameter("pc", pc).setParameter("yxzyxh", yxzyxh).list();
		List<WishList> wishs = list;
		return wishs;
	}

	public void removeYx(Integer userid, String pc, String yxzyxh) {
		// TODO Auto-generated method stub
		String HQL = "delete from  WishList  where userid = :userid and pc = :pc and yxzyxh = :yxzyxh";
		Session session = factory.getCurrentSession();
		Query query = session.createQuery(HQL);
		query.setInteger("userid", userid);
		query.setString("pc", pc);
		query.setString("yxzyxh", yxzyxh);
		query.executeUpdate();
		// session.close();
		SessionFactoryUtils.releaseSession(session, factory);
	}

}
